package com.ccbooks.bo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ccbooks.dao.BooksDao;
import com.ccbooks.dao.DBHelper;
import com.ccbooks.util.BookUtil;
import com.ccbooks.util.BooksEngine;
import com.ccbooks.view.BookListView;
import com.ccbooks.view.BookShelfView;
import com.ccbooks.vo.Book;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;

public class FileBo implements Runnable {
	
	//֧�ֵ��ļ�����
	public static final String BOOKTYPE_TXT="txt";
	public static final String BOOKTYPE_PDF="pdf";
	public static final String BOOKTYPE_HTML="html";
	public static final String BOOKTYPE_CHM="chm";
	public static final String BOOKTYPE_PDB="pdb";
	private Context context;
	public String firstPath;
	private BooksDao dao;
	
	public FileBo(Context context,String firstPath){
		this.firstPath=firstPath;
		this.context=context;
	}
	
	public FileBo(Context context,String firstPath,Dialog dialog){
		this.firstPath=firstPath;
		this.context=context;
	}
	
	//�鿴�����ļ�
	public List<String> refreshFileList(String strPath) {
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		int count=0;
		if (files == null){
			return null;
		}
		List<String> filelist=new ArrayList<String>();	
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				refreshFileList(files[i].getAbsolutePath());
			} else if(files[i].isFile()) {
				if(dao==null){
					dao=BooksDao.getDao(context);
					dao.deleteAll(DBHelper.TABLE_BOOK);
				}
				String strFileName = files[i].getAbsolutePath().toLowerCase();
				//Log.i("file", strFileName);
				String ex=FileBo.getFileEx(strFileName);
				if(BOOKTYPE_TXT.equals(ex)){
					String bookname=FileBo.getFileNa(strFileName);
					String strImgPath=strPath+"/"+bookname+".jpg";
					ContentValues cv=new ContentValues();
					cv.put("bookname", bookname);
					cv.put("bookpath",strFileName);
					cv.put("booktype", ex);
					if(new File(strImgPath).exists()){
						cv.put("imgpath", strImgPath);
					}
					dao.insert(DBHelper.TABLE_BOOK,cv);
					count++;
					//filelist.add(strFileName);
				}else if(BOOKTYPE_PDF.equals(ex)){
					
				}else if(BOOKTYPE_HTML.equals(ex)){
					
				}else if(BOOKTYPE_CHM.equals(ex)){
					
				}else if(BOOKTYPE_PDB.equals(ex)){
					String bookname=FileBo.getFileNa(strFileName);
					String strImgPath=strPath+"/"+bookname+".jpg";
					ContentValues cv=new ContentValues();
					cv.put("bookname", bookname);
					cv.put("bookpath",strFileName);
					cv.put("booktype", ex);
					if(new File(strImgPath).exists()){
						cv.put("imgpath", strImgPath);
					}
					dao.insert(DBHelper.TABLE_BOOK,cv);
					count++;
				}	
			}
		}
		BookUtil.countBooks=count;
		return filelist;
	}
	
	//批量计算文件的大小
	public void getAllFileLength(){
		if(dao==null){
			dao=BooksDao.getDao(context);
		}
		ContentValues cv=null;
		for(Book b:BooksBo.books){
			if(cv==null){
				cv =new ContentValues();
			}
			cv.clear();
			try {
				int totalBytes = 0 ;
				if(BOOKTYPE_TXT.equals(b.booktype)){
					
					totalBytes= BooksEngine.getFileSize(b.bookpath, "gbk");
				}
				cv.put(DBHelper.FILE_LENGTH, totalBytes);
				dao.update(DBHelper.TABLE_BOOK, b.id, cv);
			} catch(FileNotFoundException e){
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public static void delFile(String path) throws IOException,FileNotFoundException,Exception{
		File file=new File(path);
		if(file.exists()){
			file.delete();
		}
	}
	
	// 扩展名
	public static String getFileEx(String uri) {
		return uri.substring(uri.lastIndexOf(".") + 1, uri.length())
				.toLowerCase();
	}

	// 不带路径不带扩展名的文件名
	public static String getFileNa(String uri) {
		return uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));
	}
	
	//带路径不带扩展名的文件名
	public static String getFileRealname(String uri){
		return uri.substring(0, uri.lastIndexOf("/"));
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		refreshFileList(firstPath);
		if(context instanceof BookShelfView ){
			((BookShelfView)context).handler.sendEmptyMessage(0);
		}else if(context instanceof BookListView){
			((BookListView)context).handler.sendEmptyMessage(0);
		}
		getAllFileLength();
	}
}
